package com.guet.comment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guet.comment.beans.vo.CommentDateVO;
import com.guet.comment.mapper.CommentMapper;
import com.guet.comment.service.ICommentService;
import com.guet.common.entity.PageDataVO;
import com.guet.common.entity.PageVO;
import com.guet.user.beans.entity.User;
import com.guet.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Li
 * @create 2022-12-19-22:10
 */
@Service
public class CommentServiceImpl implements ICommentService {

    private PageVO pages = new PageVO();

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    IUserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageDataVO<CommentDateVO> page(Integer issueId, PageVO pageVO) {

        PageHelper.startPage(pageVO.getPageNum(), pageVO.getPageSize());
        List<CommentDateVO> list = commentMapper.page(issueId);

        for (CommentDateVO commentDateVO : list) {
            User user = userService.select(commentDateVO.getUserId());
            commentDateVO.setUserName(user.getUserName());
            commentDateVO.setIcon(user.getIcon());
        }

        PageInfo<CommentDateVO> pageInfo = new PageInfo<>(list);
        PageDataVO<CommentDateVO> pageDataVO = new PageDataVO<>();

        pageDataVO.setTotalPage((long) pageInfo.getPages());
        pageDataVO.setTotalItem(pageInfo.getTotal());
        pageDataVO.setPageSize((long) pageInfo.getPageSize());
        pageDataVO.setPageNum((long) pageInfo.getPageNum());
        pageDataVO.setItems(pageInfo.getList());

        return pageDataVO;

    }

    @Override
    public PageDataVO<CommentDateVO> add(Integer issueId, Integer userId, String content) {


        pages.setPageNum(1);
        pages.setPageSize(10);

        commentMapper.add(issueId, userId, content);

        PageDataVO<CommentDateVO> pageDataVO = this.page(issueId, pages);

        return pageDataVO;
    }

    @Override
    public PageDataVO<CommentDateVO> delete(Integer issueId, Integer userId, Integer id) {

        pages.setPageNum(1);
        pages.setPageSize(10);

        commentMapper.delete(issueId, userId, id);

        PageDataVO<CommentDateVO> pageDataVO = this.page(issueId, pages);

        return pageDataVO;

    }

}
